package com.cpi.cornpp.futuresversions.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.futuresversions.exception.CornFuturesVersionsException;
import com.cpi.cornpp.futuresversions.form.CornFuturesVersionsQueryForm;
import com.cpi.cornpp.futuresversions.services.CornFuturesVersionsService;

/**
 * Corn Futures Versions Query Action
 * @author Edgar Joao
 *
 */
public class CornFuturesVersionsQueryAction extends Action{
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Parametro Version Id*/
	private static final String VERSION_ID_PARAMETER = "versionId";//$NON-NLS-1$
	/**
	 * Metodo Execute
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		HttpSession httpSession = null;
		CornFuturesVersionsQueryForm futuresVersionsQueryForm = null;
		CornFuturesVersionsService cornFuturesVersionsService = null;
		List cornFuturesVersionsList = null;
		int versionId = 0;		
		String versionIdPara = null;
		try{
			futuresVersionsQueryForm = (CornFuturesVersionsQueryForm) form;
			cornFuturesVersionsList = new ArrayList(0);
			httpSession = request.getSession(false);
			cornFuturesVersionsService = new CornFuturesVersionsService();
			versionIdPara = (String)request.getParameter(VERSION_ID_PARAMETER);
			if( versionIdPara == null ){
				versionIdPara = (String)httpSession.getAttribute(VERSION_ID_PARAMETER);
			}
			versionId = Integer.parseInt(versionIdPara);			
			futuresVersionsQueryForm.setVersionId(versionId);
			
			cornFuturesVersionsList = cornFuturesVersionsService.listCornFuturesVersions(futuresVersionsQueryForm.getOrderField(), (1 == futuresVersionsQueryForm.getOrderType()) ? false : true, futuresVersionsQueryForm.getVersionId());
			if(cornFuturesVersionsList.size() > 0){
				request.setAttribute("cornFuturesVersionsList", cornFuturesVersionsList);
			}
		}catch (CornFuturesVersionsException cornFuturesVersionsException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, cornFuturesVersionsException);
			throw cornFuturesVersionsException;
		}catch (Exception e){
			CornFuturesVersionsException cornFuturesVersionsException = null;
			e.printStackTrace(System.out);
			cornFuturesVersionsException = new CornFuturesVersionsException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, cornFuturesVersionsException);
			throw cornFuturesVersionsException;
		}			
		httpSession.setAttribute("versionId", Integer.toString(versionId));
		httpSession.setAttribute("previousOrderField", Integer.toString(futuresVersionsQueryForm.getOrderField()));
		httpSession.setAttribute("previousOrderType", Integer.toString(futuresVersionsQueryForm.getOrderType()));
		httpSession.setAttribute(VERSION_ID_PARAMETER, versionIdPara);
		return mapping.findForward(DEFAULT);
	}
}
